package com.testing.month_exam_a;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

/**
 * Created by Administrator on 2017/3/1.
 */


public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder> {

    private List<Bean.RsBean> datas;
    private LayoutInflater inflater;
    Context context;
    CallBack_2 cb2;
    private int a = 0;
    private View view1;

    public void okokok(CallBack_2 cb2) {
        this.cb2 = cb2;
    }

    public RecyclerViewAdapter(Context context, List<Bean.RsBean> datas) {
        inflater = LayoutInflater.from(context);
        this.datas = datas;
        this.context = context;
    }

    //创建每一行的View 用RecyclerView.ViewHolder包装
    @Override
    public RecyclerViewAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        final View itemView = inflater.inflate(R.layout.item_left, null);

        final MyViewHolder myViewHolder = new MyViewHolder(itemView);

        myViewHolder.textview.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                final int position = myViewHolder.getAdapterPosition();
                Bean.RsBean rsBean = datas.get(position);

                cb2.myIndex(position);


                if (a == 0) {

                    view1 = itemView;
                    a++;
                }

                if (view1 != v) {
                    view1.setBackgroundResource(R.color.bg);
                    view1 = itemView;
                    itemView.setBackgroundResource(R.color.wt);
                }
            }
        });

        return myViewHolder;
    }

    //给每一行View填充数据
    @Override
    public void onBindViewHolder(RecyclerViewAdapter.MyViewHolder holder, int position) {
        holder.textview.setText(datas.get(position).dirName);


    }

    //数据源的数量
    @Override
    public int getItemCount() {
        return datas.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        private TextView textview;

        public MyViewHolder(View itemView) {
            super(itemView);
            textview = (TextView) itemView.findViewById(R.id.type);
        }
    }
}

